#import "drawundo.h"

@interface CreateGraphicsChange(PrivateMethods)

@end

@implementation CreateGraphicsChange

- initGraphicView:aGraphicView graphic:aGraphic
{
    [super init];
    graphicView = aGraphicView;
    graphic = aGraphic;
    startEditingChange = nil;

    return self;
}

- free
{
    if (![self hasBeenDone])
        [graphic free];
    if (startEditingChange)
        [startEditingChange free];
    return [super free];
}

#define NEW_STRING NXLocalStringFromTable("Operations", "New %s", NULL, "The operation of creating a new graphical entity by dragging the mouse.  The %s is one of Rectangle, Circle, etc.")

- (const char *)changeName
{
    char buffer[256];
    sprintf(buffer, NEW_STRING, [graphic title]);
    return NXUniqueString(buffer);
}

- undoChange
{
    if (startEditingChange)
        [startEditingChange undoChange];
    [graphicView removeGraphic:graphic];
    [[[NXApp inspectorPanel] delegate] loadGraphic:[graphicView selectedGraphic]]; 
    return [super undoChange];
}

- redoChange
{
    [graphicView insertGraphic:graphic];
    [[[NXApp inspectorPanel] delegate] loadGraphic:[graphicView selectedGraphic]];
    if (startEditingChange)
        [startEditingChange redoChange];
    return [super redoChange];
}

- (BOOL)incorporateChange:change
/*
 * ChangeManager will call incorporateChange: if another change
 * is started while we are still in progress (after we've 
 * been sent startChange but before we've been sent endChange). 
 * We override incorporateChange: because we want to
 * incorporate a StartEditingGraphicsChange if it happens.
 * Rather than know how to undo and redo the start-editing stuff,
 * we'll simply keep a pointer to the StartEditingGraphicsChange
 * and ask it to undo and redo whenever we undo or redo.
 */
{
    if ([change isKindOf:[StartEditingGraphicsChange class]]) {
        startEditingChange = change;
        return YES;
    } else {
        return NO;
    }
}

@end
